package com.hula.onion.book.server.spider.platforms.pengjinsuo;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by work on 2018/2/2.
 */
@Component
public class PengingSpiderHelper {
    private static final Logger logger = LoggerFactory.getLogger(PengingSpiderHelper.class);

    public Map<String, Object> installRequestParams(LoginForm loginForm) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("CI_NM", loginForm.getUserName());
        map.put("PWD", loginForm.getPassword());
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {
        if (StringUtils.isEmpty(html)) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }

        System.out.println(html);
        try {
            AccountInfo accountInfo = new AccountInfo();
            Document document = Jsoup.parse(html);
            Element element = document.selectFirst("div.total-in-details");
            String amount = element.getAllElements().get(1).text();
            System.out.println(amount);
            //accountInfo.setAmount(new BigDecimal(amount));
           // accountInfo.setYesterdayIntrest(new BigDecimal(yesterdayIntrest));
            //accountInfo.setWaitIntrest(new BigDecimal(sWI).add(new BigDecimal(iWI)));
            return new CommonResponse();
        } catch (Exception e) {
            logger.error("[PengingSpiderHelper] parseAccountBalance fail!", e);
        }
        return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
    }
}
